<?php

class ome_mdl_return_product_problem extends dbeav_model{

    function save($aData){             
        $aData['cat_path'] = $this->getCatPath($aData['parent_id']);
        //unlink(MEDIA_DIR.'/goods_cat.data');
        return parent::save($aData);
    }
    
    function updateSort($data=null){;
         if($data)
         foreach ($data as $k=>$v){
              $sql = "UPDATE `sdb_ome_return_product_problem` set p_order='".$v."' WHERE problem_id='".intval($k)."' ";
              $this->db->exec($sql);
         }
         return true;
    }
    
    function getTree(){
        return $this->db->select('SELECT cat_name AS text,problem_id AS id,parent_id AS pid,p_order,cat_path,
                    is_leaf,child_count FROM `sdb_ome_return_product_problem` order by p_order asc ');
    }
    function getCatParentById($id,$view='index'){
        if(!$id) return false;
            if(is_array($id)){
                if(implode($id,' , ')==='') return false;
                $sqlString = 'SELECT problem_id,cat_name FROM sdb_ome_return_product_problem WHERE parent_id in ('.implode($id,' , ').') order by p_order,problem_id desc';
            }else{
                $sqlString = 'SELECT problem_id,cat_name FROM sdb_ome_return_product_problem WHERE parent_id = '.$id.' order by p_order,problem_id desc';
            }
            $default_view=$view?$view:$this->app->getConf('gallery.default_view');
            $result=$this->db->select($sqlString);
            foreach($result as $cat_key=>$cat_value){
                $result[$cat_key]['link']=kernel::mkUrl('gallery',$default_view,array($cat_value['problem_id']));
            }
            return $result;
     }
    function getMap($depth=-1,$problem_id=0){
        $var_depth = $depth;
        $var_problem_id = $problem_id;
        if(isset($this->catMap[$var_depth][$var_problem_id])){
            return $this->catMap[$var_depth][$var_problem_id];
        }
        if($problem_id>0){
            $row = $this->db->select('select `cat_path` from sdb_ome_return_product_problem where problem_id='.intval($problem_id).'  order by p_order asc ');
            if($depth>0){
                $depth += substr_count($row['cat_path'],',');
            }
            $rows = $this->db->select('select cat_name,problem_id,parent_id,is_leaf,cat_path,type_id from sdb_ome_return_product_problem where cat_path like "'.$row['cat_path'].$problem_id.'%" order by p_order');
        }else{
            $rows = $this->db->select('select cat_name,problem_id,parent_id,is_leaf,cat_path,type_id from sdb_ome_return_product_problem order by p_order');
        }
        $cats = array();
        $ret = array();
        foreach($rows as $k=>$row){
            if($depth<0 || substr_count($row['cat_path'],',') < $depth){
                $cats[$row['problem_id']] = array('type'=>'gcat','parent_id'=>$row['parent_id'],'title'=>$row['cat_name'],'link'=>kernel::mkUrl('gallery','index',array($row['problem_id'])));
            }
        }
        foreach($cats as $cid=>$cat){
            if($cat['parent_id'] == $problem_id){
                $ret[] = &$cats[$cid];
            }else{
                $cats[$cat['parent_id']]['items'][] = &$cats[$cid];
            }
        }
        $this->catMap[$var_depth][$var_problem_id] = $ret;
        return $ret;
    }
    
    function getMapTree($ss=0, $str='└'){
        $var_ss = $ss;
        $var_str = $str;
        if(isset($this->catMapTree[$var_ss][$var_str])){
            return $this->catMapTree[$var_ss][$var_str];
        }
        $retCat = $this->map($this->getTree(),$ss,$str,$no,$num);
        $this->catMapTree[$var_ss][$var_str] = $retCat;
        global $step,$cat;
        $step = '';
        $cat = array();
        return $retCat;
    }
    
    /*
     * 获取类型名称
     */
    function getCatName($id=0){

//        if($id){
//            $sql = "select  firsts.cat_name firsts,sec.cat_name sec,three.cat_name three,four.cat_name four
//                    from sdb_ome_return_product_problem four
//                    left join sdb_ome_return_product_problem three on three.problem_id=four.parent_id
//                    left join sdb_ome_return_product_problem sec on sec.problem_id=three.parent_id
//                    left join sdb_ome_return_product_problem firsts on firsts.problem_id=sec.parent_id
//                    where four.problem_id='$id' ";
//            $tmp = $this->db->selectRow($sql);
//            //$tmp = $tmp['firsts']."(".$tmp['sec']."、".$tmp['three']."、".$tmp['four']."、".")";
//            $tmp = implode('<',$tmp);
//            return $tmp;
//        }
        
       if ($id==0) $cat_name_list = array();
       $filter = array('problem_id'=>$id);
       $catname = $this->dump($filter, 'problem_id,parent_id,cat_name');
       if ($catname){
           $cat_name_list .= $catname['cat_name'];
           if ($catname['parent_id']){
               $cat_name_list .= " < ";
               $cat_name_list .= $this->getCatName($catname['parent_id']);
           }
       }
       return $cat_name_list;
    }
    
    /*
     * 获取类型
     */
   function getCatList($id=0){
       
       if ($id==0) $cat_list = array();
       $filter = array('parent_id'=>$id);
       $catlist = $this->getList('problem_id,cat_name', $filter, 0, -1,'p_order');
       if ($catlist)
       foreach ($catlist as $k=>$v){
           //判断是否拥有子节点
           $childNode = $this->dump(array('parent_id'=>$v['problem_id'], 'problem_id'));
           if ($childNode['problem_id']){
               $v['childNode'] = $this->getCatList($v['problem_id']);
           }
           $cat_list[] = $v;
       }
       return $cat_list;
   }
    
    function map($data,$sID=0,$preStr='',&$cat_cuttent,&$step){
        $step++;
        $baseurl=kernel::base_url();
        $default_view=$this->app->getConf('gallery.default_view');
        if($data){
            foreach($data as $i=>$value){
                $id=$data[$i]['id'];
                $cls=($data[$i]['child_count']?'true':'false');
                $link='';//todo kernel::realUrl('gallery',$default_view,array($id),'html',$baseurl);
                if(!$sID){ //第一轮圈套
                    if(empty($data[$i]['pid'])){ //原始节点
                        $cat_cuttent[]=array(
                            'cat_name'=>$data[$i]['text'],
                            'problem_id'=>$data[$i]['id'],
                            'pid'=>$data[$i]['pid'],
                            'type'=>$data[$i]['type'],
                            'step'=>$step,
                            'p_order'=>$data[$i]['p_order'],
                            'cat_path'=>$data[$i]['cat_path'],
                            'cls'=>$cls,
                            'url'=>$link
                        );
                        unset($data[$i]);
                        $this->map($data,$id,$preStr,$cat_cuttent,$step);
                    }else{ //
                        continue;
                    }
                }else{ //子节点
                    if($sID==$data[$i]['pid']){
                        $cat_cuttent[]=array(
                            'cat_name'=>$data[$i]['text'],
                            'problem_id'=>$data[$i]['id'],
                            'pid'=>$data[$i]['pid'],
                            'type'=>$data[$i]['type'],
                            'step'=>$step,
                            'p_order'=>$data[$i]['p_order'],
                            'cat_path'=>$data[$i]['cat_path'],
                            'cls'=>$cls,
                            'url'=>$link
                        );
                        unset($data[$i]);
                        $this->map($data,$id,$preStr,$cat_cuttent,$step);
                    }
                }
            }
        }
        $step--;
        return $cat_cuttent;
    }

    function checkTreeSize(){
        $aCount = $this->db->selectrow('SELECT count(*) AS rowNum FROM sdb_ome_return_product_problem');
        if($aCount['rowNum'] > 100){
            return false;
        }else{
            return true;
        }
    }
    
    function get_cat_depth(){
        $row = $this->db->selectrow('select cat_path from sdb_ome_return_product_problem order by p_order desc');
        return count(explode(',',$row['cat_path']));
    }
    
    function cat2json($return=false){
        $contents=$this->getMapTree(0,'');
        base_kvstore::instance('template')->store('return_product_problem.data',$contents);
        if($return){
            return $contents;
        }else{
            return true;
        }
    }
    
    function getCatPath($parent_id){
        if($parent_id == 0){
            return ',';
        }
        $cat_sdf = $this->dump($parent_id);     
        return $cat_sdf['cat_path'].$cat_sdf['problem_id'].",";
    }

    function getTypeList(){ 
        $sqlString = "SELECT type_id,name FROM sdb_ome_return_product_problem WHERE disabled = 'false'";
        return $this->db->select($sqlString);
    }
    function propsort($prop=array()){
        if (is_array($prop)){
            foreach($prop as $key => $val){
                $tmpP[$val['ordernum']]=$key;
            }
            ksort($tmpP);
            return $tmpP;
        }
    }
         
     /*根据查询字符串返回UNMAE 数组
       litie@shopex.cn
     */
     function getCatLikeStr($str){
        
         if(!$str||$str !=''){
            $sql  = 'select problem_id,cat_name from '.$this->tableName.' where cat_name like "'.$str.'%" and disabled="false"';         
         }else if($str == '_ALL_'){
            $sql  = 'select problem_id,cat_name from '.$this->tableName.' where disabled="false"';
         }
        $_data = $this->db->select($sql);
        if ($_data)
        foreach($_data as $d){
            $result[] = $d['cat_name'].'&nbsp;'.$d['problem_id'];
        }
        return json_encode($result);
       
     }
     
    function get_cat_list($show_stable=false){
       return $this->cat2json(true);
    }
    
    function toRemove($catid){
        $aCats = $this->db->select('SELECT * FROM sdb_ome_return_product_problem WHERE parent_id = '.intval($catid));
        if(count($aCats) > 0){
            trigger_error(app::get('base')->_('删除失败：本分类下面还有子分类'), E_USER_ERROR);
            return false;
        }
        $row = $this->db->selectrow('SELECT parent_id FROM sdb_ome_return_product_problem WHERE problem_id='.intval($catid));
        $parent_id = $row['parent_id'];

        $this->db->exec('DELETE FROM sdb_ome_return_product_problem WHERE problem_id='.intval($catid));
        $this->cat2json();
        return true;
    }
    
    
    
    
    /*
     * 仓库入库类型
     * 
     * @return array
     */
   function store_type(){
       $store_type = array('新仓','残仓','报废');
       return $store_type;
   }
 /*
     * 仓库入库类型
     * @param int $store_type
     * @return array
     */
   function get_store_type($store_type){
    
       $store = $this->store_type();
  
       return $store[$store_type];
   }
 
}
?>